package com.xiaren.ui.tree;

import com.xiaren.common.IConstant;
import com.xiaren.common.ObjectConstant;
import com.xiaren.ui.edit.FileEdit;
import com.xiaren.ui.edit.TextFile;

import javax.sound.midi.Soundbank;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

/**
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public class TreeMouseRight implements ActionListener {
    public JPopupMenu rightPm = null;
    JMenuItem add = null;
    JMenuItem update = null;
    JMenuItem del = null;
    JMenuItem addPwd = null;
    JMenuItem delPwd = null;
    JMenuItem updatePwd = null;
    JTree jTree = null;

    public TreeMouseRight(JTree jTree) {
        super();
        this.jTree = jTree;
        init();
    }

    void init() {
        rightPm = new JPopupMenu();
        add = new JMenuItem("新建文本");
        update = new JMenuItem("编辑文本");
        del = new JMenuItem("删除文本");
        addPwd = new JMenuItem("添加密码");
        delPwd = new JMenuItem("删除密码");
        updatePwd = new JMenuItem("修改密码");

        add.addActionListener(this);
        update.addActionListener(this);
        del.addActionListener(this);
        updatePwd.addActionListener(this);
        rightPm.add(add);
        rightPm.add(update);
        rightPm.add(del);
        rightPm.add(addPwd);
        rightPm.add(delPwd);
        rightPm.add(updatePwd);

        jTree.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
            }

            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);
            }

            private void maybeShowPopup(MouseEvent e) {
                if (JTreeBiz.selectNote != null) {
                    if (e.isPopupTrigger()) {// 如果有弹出菜单
                        rightPm.show(e.getComponent(), e.getX(), e.getY());
                    } else if (JTreeBiz.rootNote != JTreeBiz.selectNote) {// 如果选择的是跟节点，则不做出响应
                        new TextFile(JTreeBiz.selectNote);
                    }
                }
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        if (this.add == e.getSource()) {
            addNoteName("节点名称");
        } else if (this.update == e.getSource()) {
            updateNoteName(JTreeBiz.selectNote.getUserObject().toString(),
                    JTreeBiz.selectNote);
        } else if (this.del == e.getSource()) {
            deleteNote(JTreeBiz.selectNote);
        } else if (this.addPwd == e.getSource()) {      // 添加密码

        } else if (this.delPwd == e.getSource()) {      // 删除密码

        } else if (this.updatePwd == e.getSource()) {      // 修改密码
            updatePwd();
        }
    }



    /**
     * 添加密码
     *
     */
    private void addPwd() {
        JPasswordField pw = new JPasswordField();
        JOptionPane.showMessageDialog(null, pw, "请输入初始化密码", JOptionPane.PLAIN_MESSAGE);
        String iniPwd = new String(pw.getPassword());



    }

    /**
     * 修改密码
     *
     */
    private void updatePwd() {
        String pwd = JTreeBiz.selectNote.getPassword();
        if(pwd == null || pwd.length() == 0){
            inputNewPwd();
        } else {
            JPasswordField pw = new JPasswordField();
            JOptionPane.showMessageDialog(null, pw, "请输入原始密码", JOptionPane.PLAIN_MESSAGE);
            String oldPwd = new String(pw.getPassword());
            pw.setText("");
            if(oldPwd != null && oldPwd.length() > 0){
                if(pwd.equals(oldPwd)){
                    inputNewPwd();
                } else {
                    JOptionPane.showMessageDialog(null, "原始密码错误,请重新输入!");
                    updatePwd();
                }
            }

        }
    }

    private void inputNewPwd(){
        JPasswordField pw = new JPasswordField();
        JOptionPane.showMessageDialog(null, pw, "请输入的初始密码", JOptionPane.PLAIN_MESSAGE);
        String inputPwd1 = new String(pw.getPassword());
        pw.setText("");
        pw.requestFocusInWindow();
        if(inputPwd1 != null && inputPwd1.length() > 0){
            JOptionPane.showMessageDialog(null, pw, "请再次输入你的密码", JOptionPane.PLAIN_MESSAGE);
            String inputPwd2 = new String(pw.getPassword());
            pw.setText("");
            if(inputPwd2 != null && inputPwd2.length() > 0){      // 点击确定
                if(inputPwd1.equals(inputPwd2)){
                    JTreeBiz.selectNote.setPassword(inputPwd1);
                    ObjectConstant.mainTree.updateUI();
                    ObjectConstant.mainFrame.baseDao.updateTree();
                    JOptionPane.showMessageDialog(null, "密码修改成功!");
                } else {
                    JOptionPane.showMessageDialog(null, "两次密码不一致,请重新输入!");
                    inputNewPwd();
                }
            }
        }
    }

    /**
     * 输入新节点的名字
     *
     * @param inputName
     */
    private void addNoteName(String inputName) {
        String noteName = JOptionPane.showInputDialog(null, "输入节点名", inputName);
        if (noteName != null) {// 如果点击的不是取消
            if ("".equals(noteName.trim())) {// 如果输入的为""给出提示
                addNoteName(inputName);
            } else {
                try {
                    JTreeBiz.selectNote.add(new JTreeNode(noteName,
                            JTreeBiz.selectNote.treeNodeId));
                    ObjectConstant.mainTree.updateUI();
                    ObjectConstant.mainFrame.baseDao.updateTree();
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, e.getMessage());
                }
            }
        }
    }

    /**
     * 修改节点名字
     *
     * @param inputName
     * @param seleteTreeNote
     */
    private void updateNoteName(String inputName, JTreeNode seleteTreeNote) {
        String noteName = JOptionPane.showInputDialog(null, "输入节点名", inputName);
        if (noteName != null) {// 如果点击的不是取消
            if ("".equals(noteName.trim())) {// 如果输入的为""给出提示
                updateNoteName(inputName, seleteTreeNote);
            } else {
                seleteTreeNote.setUserObject(noteName);
                ObjectConstant.mainTree.updateUI();
                ObjectConstant.mainFrame.baseDao.updateTree();

            }
        }
    }

    /**
     * 删除节点
     *
     * @param delNote
     */
    private void deleteNote(JTreeNode delNote) {
        int result = JOptionPane.showConfirmDialog(null, "Sure to delete?");
        if (result == JOptionPane.OK_OPTION) {
            if (delNote.isLeaf()) {
                delNote.removeFromParent();
                ObjectConstant.mainFrame.baseDao.updateTree();
                ObjectConstant.mainTree.updateUI();
                ObjectConstant.mainEdit.remove(ObjectConstant.mainEdit
                        .getSelectedComponent());
                JTreeBiz.selectNote = null;// 消除选中项
            } else {
                result = JOptionPane.showConfirmDialog(null,
                        "Under the node has child nodes, Sure to delete?");
                if (result == JOptionPane.OK_OPTION) {
                    List<JTreeNode> listNote = ObjectConstant.treeBiz
                            .noteToList(delNote);// 得到delNote下面的所有节点
                    for (JTreeNode treeNote : listNote) {// 移除节点，并且移除所选节点的所对应的面板
                        treeNote.removeFromParent();
                        Object[] array = ObjectConstant.mainEdit
                                .getComponents();
                        FileEdit fileEdit = null;
                        for (Object object : array) {
                            fileEdit = (FileEdit) object;
                            if (fileEdit.id == treeNote.treeNodeId) {
                                ObjectConstant.mainEdit.remove(fileEdit);
                            }
                        }
                    }
                    ObjectConstant.mainFrame.baseDao.updateTree();
                    ObjectConstant.mainTree.updateUI();
                    JTreeBiz.selectNote = null;// 消除选中项
                }

            }
        }
    }






}
